﻿Imports System.IO

Public Class frmBrowseFileDialog

    Private Const mcstrRootDir As String = "Computer"

    Private mstrFileDir As String
    Private mstrFileName As String
    Private mblnFileSelected As Boolean

    Public ReadOnly Property FileSelected As Boolean
        Get
            Return mblnFileSelected
        End Get
    End Property

    Public ReadOnly Property FileDir As String
        Get
            Return mstrFileDir
        End Get
    End Property

    Public ReadOnly Property FileName As String
        Get
            Return mstrFileName
        End Get
    End Property

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    Public Function fncShowForm() As Boolean

        fncShowForm = False

        Try
            Me.ShowDialog()

            Return True
        Catch ex As Exception
            clsCommon.fncShowErr("fncShowForm" & vbCrLf & ex.Message)
        End Try

    End Function

    Private Sub frmBrowseFileDialog_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Try
            If My.Settings.strDefaultDir = "" Then
                txtCurDir.Text = Environment.CurrentDirectory
            Else
                If Directory.Exists(My.Settings.strDefaultDir) Then
                    txtCurDir.Text = My.Settings.strDefaultDir
                Else
                    txtCurDir.Text = Environment.CurrentDirectory
                End If
            End If

            xListFiles()

        Catch ex As Exception
            clsCommon.fncShowErr("frmBrowseFileDialog_Load" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Function xListFiles() As Boolean

        xListFiles = False

        Try
            Dim objDir As DirectoryInfo
            Dim objSubDir As DirectoryInfo()
            Dim objSubFile As FileInfo()
            Dim objDrive As DriveInfo()

            'clear list
            lstFiles.Items.Clear()

            If txtCurDir.Text.Trim = mcstrRootDir Then

                objDrive = System.IO.DriveInfo.GetDrives()

                For i As Integer = 0 To objDrive.Count - 1
                    lstFiles.Items.Add(objDrive(i).Name)
                Next

            Else
                'change to default if directory doesnot exist
                If Not Directory.Exists(txtCurDir.Text.Trim()) Then
                    txtCurDir.Text = Environment.CurrentDirectory
                End If

                'get data
                objDir = New DirectoryInfo(txtCurDir.Text.Trim())
                objSubDir = objDir.GetDirectories()
                objSubFile = objDir.GetFiles()

                'fill list
                For i As Integer = 0 To objSubDir.Count - 1
                    lstFiles.Items.Add(objSubDir(i).Name)
                Next

                For i As Integer = 0 To objSubFile.Count - 1
                    lstFiles.Items.Add(objSubFile(i).Name)
                Next

            End If

            'clear all value
            mstrFileDir = ""
            mstrFileName = ""
            txtFileName.Text = ""

            Return True
        Catch ex As Exception
            clsCommon.fncShowErr("xListFiles" & vbCrLf & ex.Message)
        End Try

    End Function

    Private Sub btnUp_Click(sender As System.Object, e As System.EventArgs) Handles btnUp.Click
        Try
            Dim objDir As DirectoryInfo

            If txtCurDir.Text.Trim() = mcstrRootDir Then Exit Sub

            'change to default if directory doesnot exist
            If Not Directory.Exists(txtCurDir.Text.Trim()) Then
                txtCurDir.Text = Environment.CurrentDirectory
            End If

            'get data
            objDir = New DirectoryInfo(txtCurDir.Text.Trim())

            If objDir.Parent Is Nothing Then
                txtCurDir.Text = mcstrRootDir
            Else
                txtCurDir.Text = objDir.Parent.FullName
            End If

            xListFiles()

        Catch ex As Exception
            clsCommon.fncShowErr("btnUp_Click" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub lstFiles_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstFiles.SelectedIndexChanged
        Try
            If lstFiles.SelectedIndex < 0 Then Exit Sub

            Dim strFilePath As String
            Dim objFile As FileInfo

            strFilePath = Path.Combine(txtCurDir.Text.Trim(), lstFiles.SelectedItem.ToString())
            objFile = New FileInfo(strFilePath)

            'exit if this is not a file or file doesnot exist
            'If Not File.Exists(strFilePath) Then Exit Sub
            If Not objFile.Exists Then Exit Sub

            txtFileName.Text = objFile.Name
            mstrFileDir = objFile.FullName
            mstrFileName = objFile.Name

        Catch ex As Exception
            clsCommon.fncShowErr("lstFiles_SelectedIndexChanged" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub lstFiles_MouseDoubleClick(sender As System.Object, e As System.Windows.Forms.MouseEventArgs) Handles lstFiles.MouseDoubleClick
        Try
            If lstFiles.SelectedIndex < 0 Then Exit Sub

            Dim strFilePath As String
            Dim objDir As DirectoryInfo

            If txtCurDir.Text.Trim() = mcstrRootDir Then txtCurDir.Text = ""

            strFilePath = Path.Combine(txtCurDir.Text.Trim(), lstFiles.SelectedItem.ToString())
            objDir = New DirectoryInfo(strFilePath)

            'exit if this is not a folder or folder doesnot exist
            'If Not File.Exists(strFilePath) Then Exit Sub
            If Not objDir.Exists Then Exit Sub

            txtCurDir.Text = strFilePath
            xListFiles()

        Catch ex As Exception
            clsCommon.fncShowErr("lstFiles_MouseDoubleClick" & vbCrLf & ex.Message)
        End Try
    End Sub



    Private Sub btnChoose_Click(sender As System.Object, e As System.EventArgs) Handles btnChoose.Click
        Try
            If txtFileName.Text.Trim() = "" Then
                clsCommon.fncShowWarning("You need to select a file.", lstFiles)
                Exit Sub
            End If

            mblnFileSelected = True
            My.Settings.strDefaultDir = txtCurDir.Text.Trim()
            My.Settings.Save()
            Me.Close()

        Catch ex As Exception
            clsCommon.fncShowErr("btnChoose_Click" & vbCrLf & ex.Message)
        End Try
    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        Try

            Me.Close()

        Catch ex As Exception
            clsCommon.fncShowErr("btnCancel_Click" & vbCrLf & ex.Message)
        End Try
    End Sub

End Class